package org.ecpkn.PAGridSim.FastOptimizationWithoutSimulation.LargestTaskFirst;

import java.util.ArrayList;
import java.util.Collections;

import org.ecpkn.PAGridSim.core.gridsimEntity.paMachine.ComparatorMachineUtilization;
import org.ecpkn.PAGridSim.core.gridsimEntity.paMachine.PAMachine;
import org.ecpkn.PAGridSim.core.offlineOptimization.workloadBalance.ComparatorTaskUtilization;
import org.ecpkn.PAGridSim.core.tasksCreation.Task;

public class LTFPAPolicy {
	// TODO:adapt to a real PAPolicy who herite the PAPolicy
	// Let different PAPolicy choose different xml
	public static void schedule(int machineNumber, ArrayList<Task> tasks)
			throws Exception {
		System.out.println(machineNumber);
		ComparatorTaskUtilization comparator = new ComparatorTaskUtilization();
		Collections.sort(tasks, comparator);
		ArrayList<PAMachine> ml = new ArrayList<PAMachine>();

		for (int index = 0; index < machineNumber; index++) {
			PAMachine m = new PAMachine(index, 1, (int) PAMachine.MAXRATING);
			ml.add(m);
		}
		ComparatorMachineUtilization c = new ComparatorMachineUtilization();
		for (Task t : tasks) {
			// System.out.println("taskU"+t.getUtilization(TaskFactory.STANDARDRATING));
			PAMachine m = Collections.min(ml, c);
			m.addTaskToMachine(t);
			m.changeSingleTaskMachineUtilization(t
					.getUtilization(PAMachine.MAXRATING));
		}
		double sumPower = 0;
		for (PAMachine m : ml) {
			// System.out.println("machineU"+m.getMachinesUtilization());
			m.setFrequency(1.0,m.getMachinesUtilization()
					* PAMachine.MAXRATING);
			sumPower = sumPower + m.getPower();
		}
		System.out.println(sumPower);
	}
}
